package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageBean;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;


    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    DishMapper dishMapper;

    @Override
    public PageBean getPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = (Page<SetmealVO>) setmealMapper.pageSetmeal(setmealPageQueryDTO);

        PageBean pageBean = new PageBean();
        pageBean.setTotal(page.getTotal());
        pageBean.setRecords(page.getResult());
        return pageBean;

    }

    @Transactional
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        // 将DTO 给 VO
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.updateSetmeal(setmeal);
        // 获取套餐的id
        //Long id = setmealMapper.getId(setmealDTO.getName());
        setmealDishMapper.deleteSetmealDishByIds(Collections.singletonList(String.valueOf(setmealDTO.getId())));
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        if (setmealDishes != null && setmealDishes.size() > 0) {
            for (SetmealDish setmealDish : setmealDishes) {
                // 未起售的菜品不能加入
                Integer flag = dishMapper.queryStatus(setmealDish.getDishId());
                if (flag.equals(StatusConstant.DISABLE)) {
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
                setmealDish.setSetmealId(setmealDTO.getId());
            }
            setmealDishMapper.addSetmealDish(setmealDishes);
        }


    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public SetmealVO getSetmealById(String id) {
        Setmeal setmeal = setmealMapper.getSetmealById(id);
        //根据菜品id查询套餐菜品数据

        List<SetmealDish> setmealDishes = setmealDishMapper.getSetmealDishById(id);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;


    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        // 查询菜品的状态
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.addSetmeal(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        for (SetmealDish setmealDish : setmealDishes) {
            // 未起售的菜品不能加入
            Integer flag = dishMapper.queryStatus(setmealDish.getDishId());
            if (flag.equals(StatusConstant.DISABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
            setmealDish.setSetmealId(setmeal.getId());
        }
        setmealDishMapper.addSetmealDish(setmealDishes);
    }

    @Override
    public void changeStatus(String status, String id) {
        List<Long> dishId = setmealDishMapper.getDishId(id);
        Integer dishStatusCount = dishMapper.queryStatusDishes(dishId);
        if (dishStatusCount > 0) {
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
        }
        Setmeal setmeal = new Setmeal();
        setmeal.setId(Long.valueOf(id));
        setmeal.setStatus(Integer.valueOf(status));
        setmealMapper.changeStatus(setmeal);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteSetmeals(List<String> ids) {
        //1. 在售中的套餐不能删除
        Integer countOnsale = setmealMapper.statisticsOnSale(ids);
        if (countOnsale > 0) {
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }

        setmealMapper.deleteSetmeal(ids);
        // 删除setmeal_dish 关联的菜品
        setmealDishMapper.deleteSetmealDishByIds(ids);
    }

    @Override
    public List<Setmeal> getSetmealByCategoryId(String categoryId) {
        return setmealMapper.getSetmealByCategoryId(categoryId);
    }




}
